// pages/goods/detail.js
var app = getApp();
var a = app.requirejs("core");
var WxParse = app.requirejs("wxParse/wxParse");
Page({
  data: {
    scrollTop:0,
    num:1,
    show:false
  },
  onLoad: function (options) {
    var self = this;
    var gid = this.gid = options.gid ? options.gid:options.id;
    console.log(gid);
    var systemInfo = wx.getSystemInfoSync();
    a.get('Pintuan/ptGoodsDetail',{
        gid:gid,
      uid: app.getCache("userinfo").uid
    },function(t){
      console.log(t)
      if(t.code==0){
        if(t.info.intro){
          WxParse.wxParse("wxParseData", "html", t.info.intro, self, "0");
        }
        self.setData({
          windowHeight: systemInfo.windowHeight,
          goodsDetail: t.info
        })
        var groupList = t.info.groupList;
        if (groupList.length > 0) {
          for (var i = 0; i < groupList.length; i++) {
            var t = --groupList[i].leftTime;
            var h = Math.floor(t / 60 / 60);
            var m = Math.floor((t - h * 60 * 60) / 60);
            var s = t % 60;
            if (h < 10) h = "0" + h;
            if (m < 10) m = "0" + m;
            if (s < 10) s = "0" + s;
            groupList[i].leftTimeStr = h + ':' + m + ':' + s
          }
          self.setTimeData(groupList);
        }
        self.setData({
          groupList, groupList,
          show:true
        })
      }else{
        a.alert(t.msg);
      }
    },true)
  },
  onShareAppMessage: function (res) {
    return {
      title: this.goodsDetail.name,
      path: "/yb_shop/pages/pintuan/pages/goods/detail?gid=" + this.goodsDetail.id,
      success:function(res){
        console.log(res)
      } 
    }
  },
  setTimeData:function(data){
    var self = this;
    var groupList = data;
    setInterval(function(){
      for (var i = 0; i < groupList.length; i++) {
        var t = --groupList[i].leftTime;
        var h =  Math.floor(t/60/60);
        var m = Math.floor((t-h*60*60)/60);
        var s = t%60;
        if(h<10) h = "0"+h;
        if(m<10) m = "0"+m;
        if(s<10) s = "0"+s;
        groupList[i].leftTimeStr = h+':'+m+':'+s
      }
      self.setData({
        groupList:groupList
      })
    }, 1000)
  },
  joinGroup:function(e){
    var id = e.currentTarget.dataset.id;
    app.redirect('group/detail','id='+id);
  },
  goHome:function(){
    a.jump('/yb_shop/pages/pintuan/pages/index/index',2);
  },
  goToBuy:function(){
    var goodsDetail = this.data.goodsDetail;
    goodsDetail['num'] = this.data.num;
    goodsDetail['goodsPrice'] = this.data.goodsPrice;
    goodsDetail['buyType'] = this.data.buyType;
    goodsDetail['groupPid'] = 0;
    app.goodsInfo = goodsDetail; 
  
    app.redirect('goods/payfor');
  },
  selectProp:function(e){
    var current = e.currentTarget.dataset;
    var pid = current.pid;
    var pname = current.pname;
    var name = current.name;
    var propValue = this.propValue ? this.propValue : [];
    propValue[pid] = {pname:pname,name:name};
    this.propValue = propValue;
    this.setData({
      propValue:propValue
    });
  },
  minus:function(){
    var num = this.data.num > 1 ? --this.data.num : 1
    this.setData({
      num : num
    });
  },
  plus:function(){
    var num = ++this.data.num
    this.setData({
      num : num
    });
  },
  showModal:function(e){
    var type = e.currentTarget.dataset.type;
    var showModalStatus = e.currentTarget.dataset.statu== 'open' ? true : false;
    var goodsPrice = type == 'group' ? this.data.goodsDetail.gprice : this.data.goodsDetail.price;
    var buyType = type=='group'? 1 : 0;
    app.showModal(this);
    this.setData({
      showModalStatus:showModalStatus,
      goodsPrice:goodsPrice,
      buyType:buyType
    });
  },
  scrolltolower:function(){

  },
  showServer:function(e){
    // var showServer = e.currentTarget.dataset.statu== 'open' ? true : false;
    // app.showModal(this)
    // this.setData({
      // showServer:showServer
    // })
  }
});